package com.unbank.util;

public class UrlRuleUtil {
	// 得到实际的URL
	public static String getcheckURL(String url, int i) {
		String url_rule = "";
		if (i < 10 && url.contains("0%PAGE")) {
			url_rule = url.replace("0%PAGE", "0" + i);
		} else {
			url_rule = url.replace("%PAGE", i + "");
		}
		return url_rule;
	}

	// 通过比较两个URL 得到规则（只适用与 01 02 | 1 2 | 9 10| 09 10）
	/**
	 * 
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static String getURlRule(String a, String b) {
		a = a.trim();
		b = b.trim();
		char[] array1 = a.toCharArray();
		char[] array2 = b.toCharArray();
		int num = a.length();
		if (a.length() > b.length()) {
			num = b.length();
		} else {
			num = a.length();
		}
		StringBuffer sb = new StringBuffer();
		int index = 0;
		for (int i = 0; i < num; i++) {
			if (array1[i] == array2[i]) {
				if (index != 0) {
					break;
				}
				continue;
			} else {
				if (index == 0) {
					index = i;
				}
				sb.append(array1[i]);
			}
		}
		int indexOf = sb.length();
		if (indexOf == 0) {
			return a;
		}
		String before = a.substring(0, index);
		String after = a.substring(index + indexOf);
		String ruleURL = "";
		if (before.endsWith("0")) {
			ruleURL = before.substring(0, before.length() - 1) + "0%PAGE"
					+ after;
		} else {
			ruleURL = before + "%PAGE" + after;
		}
		return ruleURL;
	}

	public static void main(String[] args) {
		System.out.println(getURlRule("a", "a"));
	}

}
